<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit">
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['serviceId']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="服务中文名">
        <a-input placeholder="服务中文名" v-decorator="['name',{rules: [{required: true, message: '请输入服务中文名'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="服务英文名">
        <a-input placeholder="请输入服务英文名，生成路径使用 如：project" v-decorator="['moduleName',{rules: [{required: true, message: '请输入服务英文名 如：project'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="服务工程名">
        <a-input placeholder="请输入服务工程名 如：ruoyi-project" v-decorator="['code',{rules: [{required: true, message: '请输入服务工程名 如：ruoyi-project'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="服务包路径">
        <a-input placeholder="请输入服务包路径 如：com.ruoyi.project" v-decorator="['packageName',{rules: [{required: true, message: '请输入服务包路径 如：com.ruoyi.project'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="服务网关标志">
        <a-input placeholder="请输入服务网关标志 如：api-project" v-decorator="['gatewayName',{rules: [{required: true, message: '请输入服务网关标志 如：api-project'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
        <a-input placeholder="请输入备注" v-decorator="['remark',{rules: [{required: true, message: '请输入备注'}]}]"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import moment from "moment";
import { dictList,getDictMapKey,tableDictList,getTableDictKey } from '@/utils/dict'
import { saveGenService,getGenServiceInfo } from '@/api/gen/service'
import pick from 'lodash.pick'

export default {
  name: 'GenServiceModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
    this.dictSync();
  },
  methods: {
    async dictSync(){
    } ,
    add (query) {
      this.form.resetFields()
        if(query) {
            this.query=query;
            this.mdl = Object.assign(this.mdl,this.query)
        }
      this.edit()
    },
    edit (serviceId,query) {
      if(query){
        this.query=query;
      }
      if(serviceId){
        getGenServiceInfo(serviceId).then(res => {
          console.log("info:"+res)
          console.log(JSON.stringify(res))
          this.mdl = Object.assign(res)
          this.mdl = Object.assign(this.mdl,this.query)
          this.visible = true
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.mdl,'serviceId','code','packageName','gatewayName','moduleName','name','remark'))
          })
        }).then(res => {
          console.log("errpr:"+res)
          console.log("errpr:"+JSON.stringify(res))
        })
      }else{
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl,'code','packageName','gatewayName','moduleName','name','remark'))
        })
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveGenService(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.success(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
